package networkresourcescanner;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on Jun 27, 2011, 2:57:59 AM
 */


import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hirayami
 */
public class GUI extends javax.swing.JFrame {

    /** Creates new form GUI */
    public GUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        menuPanel = new javax.swing.JPanel();
        filterByShareButton = new javax.swing.JButton();
        expandAllButton = new javax.swing.JButton();
        statusPanel = new javax.swing.JPanel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jSlider1 = new javax.swing.JSlider();
        InputPanel = new javax.swing.JPanel();
        rangeInput = new javax.swing.JTextField();
        scanButton = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        contentSplitPane = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeVung = new networkresourcescanner.MyTree();
        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        menuPanel.setBackground(new java.awt.Color(222, 240, 117));
        menuPanel.setPreferredSize(new java.awt.Dimension(527, 40));

        filterByShareButton.setText("FilterByShare");
        filterByShareButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterByShareButtonActionPerformed(evt);
            }
        });

        expandAllButton.setText("ExpandAll");
        expandAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expandAllButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
        menuPanel.setLayout(menuPanelLayout);
        menuPanelLayout.setHorizontalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(filterByShareButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(expandAllButton)
                .addContainerGap(942, Short.MAX_VALUE))
        );
        menuPanelLayout.setVerticalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(filterByShareButton)
                    .addComponent(expandAllButton))
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(menuPanel, gridBagConstraints);

        statusPanel.setBackground(new java.awt.Color(234, 189, 121));

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(765, Short.MAX_VALUE)
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(statusPanel, gridBagConstraints);

        InputPanel.setBackground(new java.awt.Color(237, 226, 209));
        InputPanel.setLayout(new java.awt.GridBagLayout());

        rangeInput.setColumns(20);
        rangeInput.setText("10.8.11.0-255");
        rangeInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rangeInputActionPerformed(evt);
            }
        });
        InputPanel.add(rangeInput, new java.awt.GridBagConstraints());

        scanButton.setText("Scan");
        scanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scanButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        InputPanel.add(scanButton, gridBagConstraints);

        jTextField2.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        InputPanel.add(jTextField2, gridBagConstraints);

        jLabel1.setText("Search");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        InputPanel.add(jLabel1, gridBagConstraints);

        jLabel2.setText("     ");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        InputPanel.add(jLabel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(InputPanel, gridBagConstraints);

        contentSplitPane.setDividerLocation(350);
        contentSplitPane.setDividerSize(3);
        contentSplitPane.setResizeWeight(0.5);

        jScrollPane1.setViewportView(treeVung);

        contentSplitPane.setLeftComponent(jScrollPane1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(contentSplitPane, gridBagConstraints);

        jMenu1.setText("File");
        menuBar.add(jMenu1);

        jMenu2.setText("Edit");
        menuBar.add(jMenu2);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void scanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanButtonActionPerformed
        // TODO add your handling code here:

        String str = rangeInput.getText();
        StringTokenizer strtoc = new StringTokenizer(str, ".");
        //str.s
        W.P(str);
        try {
            treeVung.startScanning(extractIPInput(rangeInput.getText()), 50);
        } catch (Exception ex) {
            W.P("input vul hoise grrr,.......");
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_scanButtonActionPerformed

    private void filterByShareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterByShareButtonActionPerformed
        // TODO add your handling code here:
        treeVung.filterByShare();
    }//GEN-LAST:event_filterByShareButtonActionPerformed

    private void rangeInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rangeInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rangeInputActionPerformed

    private void expandAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expandAllButtonActionPerformed
        // TODO add your handling code here:
        treeVung.expandAllNode();
    }//GEN-LAST:event_expandAllButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel InputPanel;
    private javax.swing.JSplitPane contentSplitPane;
    private javax.swing.JButton expandAllButton;
    private javax.swing.JButton filterByShareButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel menuPanel;
    private javax.swing.JTextField rangeInput;
    private javax.swing.JButton scanButton;
    private javax.swing.JPanel statusPanel;
    private networkresourcescanner.MyTree treeVung;
    // End of variables declaration//GEN-END:variables

    private String[] extractIPInput(String src) throws Exception {


        StringTokenizer strtoc = new StringTokenizer(src, ".");


        if (strtoc.countTokens() != 4) {
            throw new Exception("input vul");
        }

        String[] fields = new String[4];

        int i = 0;
        while (strtoc.hasMoreTokens()) {
            fields[i] = strtoc.nextToken();
            i++;
        }

        // TODO: check ip v6 or others..


        W.P("input ok");
        Integer[] arr = new Integer[4];

        Integer[][] a = new Integer[4][];

        for (i = 0; i < 4; i++) {
            a[i] = getIpRange(fields[i]);
            //W.PA(a[i]);
        }
        return getAll(a);
    }

    private Integer[] getIpRange(String string) throws Exception {

        ArrayList<Integer> ret = new ArrayList<Integer>();
        StringTokenizer commaSeparated = new StringTokenizer(string, ",");
        int i, j, a, b;


        for (i = 0; commaSeparated.hasMoreTokens(); i++) {
            StringTokenizer hifSeparated = new StringTokenizer(commaSeparated.nextToken(), "-");

            if (hifSeparated.countTokens() == 1) {
                j = Integer.parseInt(hifSeparated.nextToken());
                //W.P("j:" + j);
                if (j >= 0 && j <= 255) {
                    ret.add(j);
                }
            } else if (hifSeparated.countTokens() == 2) {
                a = Integer.parseInt(hifSeparated.nextToken());
                b = Integer.parseInt(hifSeparated.nextToken());

                //W.P("a:" + a);
                //W.P("b:" + b);
                for (j = a; j <= b; j++) {
                    if (j >= 0 && j <= 255) {
                        ret.add(j);
                    }
                }
            } else {
                throw new Exception("input vul");
            }
        }
        Integer[] r = new Integer[ret.size()];
        i = 0;
        for (Integer integer : ret) {
            r[i] = integer;
            i++;
        }
        return r;
    }

    private String[] getAll(Integer[][] a) {
        ArrayList<String> ret = new ArrayList<String>();
        for (int i = 0; i < a[0].length; i++) {
            for (int j = 0; j < a[1].length; j++) {
                for (int k = 0; k < a[2].length; k++) {
                    for (int l = 0; l < a[3].length; l++) {
                        ret.add(a[0][i] + "." + a[1][j] + "." + a[2][k] + "." + a[3][l]);
                    }
                }

            }
        }
        String[] r = new String[ret.size()];
        int i = 0;
        for (String str : ret) {
            r[i] = str;
            i++;
        }
        return r;
    }
}
